package com.entfrm.biz.it.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.entfrm.biz.it.entity.RepairEmail;
import com.entfrm.biz.it.manager.RepairEmailManager;
import com.entfrm.biz.it.service.RepairEmailService;
import com.entfrm.core.log.annotation.OperLog;
import org.springframework.security.access.prepost.PreAuthorize;
import com.entfrm.core.base.api.R;
import com.entfrm.core.base.util.ExcelUtil;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;
import java.util.Arrays;

/**
 * @author kyc
 * @date 2020-08-17 09:19:43
 * @description 报修邮箱配置Controller
 */
@Api("报修邮箱配置管理")
@RestController
@AllArgsConstructor
@RequestMapping("/it/repairEmail")
public class RepairEmailController {

    private final RepairEmailService repairEmailService;

    private final RepairEmailManager repairEmailManager;

    private QueryWrapper<RepairEmail> getQueryWrapper(RepairEmail repairEmail) {
        return new QueryWrapper<RepairEmail>()
                .eq(StrUtil.isNotBlank(repairEmail.getEmailAccount()), "email_account", repairEmail.getEmailAccount())
                .like(StrUtil.isNotBlank(repairEmail.getEmailHostname()), "email_hostname", repairEmail.getEmailHostname())
                .eq(StrUtil.isNotBlank(repairEmail.getIsUse()), "is_use", repairEmail.getIsUse())
                .orderByDesc("create_time");
    }

    @ApiOperation("报修邮箱配置列表")
    @PreAuthorize("@ps.hasPerm('repairEmail_view')")
    @GetMapping("/list")
    public R list(Page page, RepairEmail repairEmail) {
        IPage<RepairEmail> repairEmailPage = repairEmailService.page(page, getQueryWrapper(repairEmail));
        return R.ok(repairEmailPage.getRecords(), repairEmailPage.getTotal());
    }

    @ApiOperation("报修邮箱配置查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Integer id) {
        return R.ok(repairEmailService.getById(id));
    }

    @OperLog("报修邮箱配置新增")
    @ApiOperation("报修邮箱配置新增")
    @PreAuthorize("@ps.hasPerm('repairEmail_add')")
    @PostMapping("/save")
    public R save(@Validated @RequestBody RepairEmail repairEmail) {
        repairEmailService.save(repairEmail);
        return R.ok();
    }

    @OperLog("报修邮箱配置修改")
    @ApiOperation("报修邮箱配置修改")
    @PreAuthorize("@ps.hasPerm('repairEmail_edit')")
    @PutMapping("/update")
    public R update(@Validated @RequestBody RepairEmail repairEmail) {
        repairEmailService.updateById(repairEmail);
        return R.ok();
    }


    @OperLog("报修邮箱配置删除")
    @ApiOperation("报修邮箱配置删除")
    @PreAuthorize("@ps.hasPerm('repairEmail_del')")
    @DeleteMapping("/remove/{ids}")
    public R remove(@PathVariable("ids") Integer[] ids) {
        return R.ok(repairEmailService.removeByIds(Arrays.asList(ids)));
    }

    @OperLog("使用报修邮箱")
    @ApiOperation("使用报修邮箱")
    @PreAuthorize("@ps.hasPerm('repairEmail_use')")
    @PutMapping("/useRepairEmail")
    public R useRepairEmail(@RequestParam("id") Integer id) {
        repairEmailManager.useRepairEmail(id);
        return R.ok();
    }


    @PreAuthorize("@ps.hasPerm('repairEmail_export')")
    @GetMapping("/export")
    public R export(RepairEmail repairEmail) {
        List<RepairEmail> list = repairEmailService.list(getQueryWrapper(repairEmail));
        ExcelUtil<RepairEmail> util = new ExcelUtil<RepairEmail>(RepairEmail.class);
        return util.exportExcel(list, "报修邮箱配置数据");
    }
}
